package net.zoneland.data.dataprofiling.common.service.impl;

import net.zoneland.data.dataprofiling.common.domain.FileEntity;
import net.zoneland.data.dataprofiling.common.domain.FileQuery;
import net.zoneland.data.dataprofiling.common.repository.FileRepository;
import net.zoneland.data.dataprofiling.common.service.FileService;
import net.zoneland.data.dataprofiling.common.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private FileRepository fileRepository;

    @Override
    public FileEntity get(Long id) {
        return fileRepository.getOne(id);
    }

    @Override
    public PageUtils list(int page, int size, FileQuery fileQuery) {
        Pageable pageable = PageRequest.of(page,size);
        Page<FileEntity> fileEntityPage = fileRepository.findAll(pageable);
        PageUtils pageUtils = new PageUtils(fileEntityPage.getContent(), (int) fileEntityPage.getTotalElements());
        return pageUtils;
    }


    @Override
    public int count(Map<String, Object> map) {
        return 0;
    }

    @Override
    public int save(FileEntity sysFile) {
        FileEntity fileEntity = fileRepository.save(sysFile);
        if(fileEntity!=null){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public int update(FileEntity sysFile) {
        FileEntity fileEntity = fileRepository.saveAndFlush(sysFile);
        if(fileEntity!=null){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public int remove(Long id) {
        fileRepository.deleteById(id);
        return 1;
    }

    @Override
    public int batchRemove(Long[] ids) {
        for(Long id:ids){
            remove(id);
        }
        return ids.length;
    }

    @Override
    public Boolean isExist(String url) {
        FileEntity fileEntity = fileRepository.findFirstByUrl(url);
        if(fileEntity!=null){
            return true;
        }else {
            return false;
        }
    }
}
